package com.trainsmart.example.demo.control
{
	
	import com.trainsmart.example.demo.model.UserVO;
	import com.trainsmart.smartmvc.Context;
	import com.trainsmart.smartmvc.IControl;
	import com.trainsmart.smartmvc.IModel;
	/**
	 * add user and refresh dataGrid
	 * register static in module.xml
	 */ 
	public class AddUserCtrl implements IControl
	{
		private var _context:Context = null;
		
		public function AddUserCtrl(context:Context)
		{
			_context = context;
		}
		/**
		 * need to declair request structure 
		 * request={userId, userName}
		 * 
		 */ 
		public function execute(request:Object=null, response:Function=null):void{
			var vo:UserVO = new UserVO();
			vo.userId= request["userId"] as String;
			vo.name = request["userName"] as String;
			var scope:String = request["scope"] as String;
			saveUser(vo,scope);			
		}
		/**
		 * step1 : save vo in db
		 */ 
		private function saveUser(vo:UserVO,scope:String ):void{
			var model:IModel = _context.getModel("userdao");
			if(scope=='global')			
			model.save(vo, refreshAll);
			else
			model.save(vo, refreshDG);
		}
		
		private function refreshDG():void{
			
			_context.sendCommand("refresh");
		}
		private function refreshAll():void{
			_context.sendCommandX("refresh");
		}
		
	}
}